package org.tigr.htc.cmd;

/*
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

import java.util.EventListener;

import org.tigr.antware.shared.command.events.CmdEventDispatcher;

/**
 * The <b>HTCEventDispatcher</b> class manages a list of command listeners. 
 * This class can handle the logistics of sending events and adding/removing 
 * listeners.
 */

public class HTCEventDispatcher extends CmdEventDispatcher {

	/**
	 * Creates a new <code>HTCEventDispatcher</code> instance. 
	 */
	public HTCEventDispatcher() {
		super();
	}
	
	/**
	 *  <code>addSubmitListener</code> .
	 *
	 * @param pListener a <code>CommandRuntimeStatusLI</code> value
	 */
	public void addTaskListener(CommandTaskLI pListener) {
		list.add(CommandTaskLI.class, pListener);
	}
	
	/**
	 * @param pListener
	 */
	public void removeTaskListener(CommandTaskLI pListener) {
		list.remove(CommandTaskLI.class, pListener);
	}
	
	/**
	 * @param pStartEvent
	 */
	public void fireTkStartEvent(TkStartEvent pStartEvent) {
		// Send start notifications to all status listeners
		EventListener[] listeners = list.getListeners(CommandTaskLI.class);
		
		// Process the listeners last to first, notifying
		if (listeners != null) {
			for (int i = listeners.length - 1; i >= 0; i--) {
				((CommandTaskLI) listeners[i]).taskStarted(pStartEvent);
			}
		}
	}
	
	/**
	 * @param pStartEvent
	 */
	public void fireTkRestartEvent(TkStartEvent pStartEvent) {
		EventListener[] listeners = list.getListeners(CommandTaskLI.class);
		// Process the listeners last to first, notifying
		if (listeners != null) {
			for (int i = listeners.length - 1; i >= 0; i--) {
				((CommandTaskLI) listeners[i]).taskRestarted(pStartEvent);
			}
		}
	}
	
	/**
	 * @param pFinishEvent
	 */
	public void fireTkFinishEvent(TkFinishEvent pFinishEvent) {
		// Send finish notifications to all status listeners
		EventListener[] listeners = list.getListeners(CommandTaskLI.class);
		
		// Process the listeners last to first, notifying
		if (listeners != null) {
			for (int i = listeners.length - 1; i >= 0; i--) {
				((CommandTaskLI) listeners[i]).taskFinished(pFinishEvent);
			}
		}
	}
	
	/**
	 * @param pInterruptEvent
	 */
	public void fireTkInterruptEvent(TkInterruptEvent pInterruptEvent) {
		EventListener[] listeners = list.getListeners(CommandTaskLI.class);
		// Process the listeners last to first, notifying
		if (listeners != null) {
			for (int i = listeners.length - 1; i >= 0; i--) {
				((CommandTaskLI) listeners[i]).taskInterrupted(pInterruptEvent);
			}
		}
	}
	
	public void fireTkFailureEvent(TkFailureEvent pFailureEvent) {
		EventListener[] listeners = list.getListeners(CommandTaskLI.class);
		// Process the listeners last to first, notifying
		if (listeners != null) {
			for (int i = listeners.length - 1; i >= 0; i--) {
				((CommandTaskLI) listeners[i]).taskFailed(pFailureEvent);
			}
		}
		
	}
	
	/**
	 * @param pResumeEvent
	 */
	public void fireTkResumeEvent(TkResumeEvent pResumeEvent) {
		EventListener[] listeners = list.getListeners(CommandTaskLI.class);
		// Process the listeners last to first, notifying
		if (listeners != null) {
			for (int i = listeners.length - 1; i >= 0; i--) {
				((CommandTaskLI) listeners[i]).taskResumed(pResumeEvent);
			}
		}
	}
	
	/**
	 * @param pSuspendEvent
	 */
	public void fireTkSuspendEvent(TkSuspendEvent pSuspendEvent) {
		EventListener[] listeners = list.getListeners(CommandTaskLI.class);
		// Process the listeners last to first, notifying
		if (listeners != null) {
			for (int i = listeners.length - 1; i >= 0; i--) {
				((CommandTaskLI) listeners[i]).taskSuspended(pSuspendEvent);
			}
		}
	}
	
	/**
	 * @param pSubmitEvent
	 */
	public void fireTkSubmitEvent(TkSubmitEvent pSubmitEvent) {
		EventListener[] listeners = list.getListeners(CommandTaskLI.class);
		// Process the listeners last to first, notifying
		if (listeners != null) {
			for (int i = listeners.length - 1; i >= 0; i--) {
				((CommandTaskLI) listeners[i]).taskSubmitted(pSubmitEvent);
			}
		}
	}
}

